import { ref } from 'vue'
import { typeSave } from '@/api/goodsSet'
import { AddCommodityTypeForm } from './types'

/**
 * 添加设备类型
 */
const useAddCommodityType = () => {
  // 添加类型加载
  const addCommodityTypeLoading = ref(false)

  /**
   * 添加类型
   */
  const addCommodityType = async () => {
    addCommodityTypeLoading.value = true
    const res = await typeSave(addCommodityTypeForm.value)
    addCommodityTypeLoading.value = false
    return res.data as { code: number; data: object; message: string; success: boolean }
  }

  /**
   * 类型表单
   */
  const addCommodityTypeForm = ref<AddCommodityTypeForm>({
    id: null,
    name: '',
    parentId: '',
    status: false,
    childData: [],
    visibilityType: 1,
  })

  /**
   * 重置类型表单
   */
  const resetAddCommodityTypeForm = () => {
    addCommodityTypeForm.value = {
      id: null,
      name: '',
      parentId: '',
      status: false,
      childData: [],
      visibilityType: 1,
    }
  }

  return {
    addCommodityTypeForm,
    addCommodityTypeLoading,
    addCommodityType,
    resetAddCommodityTypeForm,
  }
}

export default useAddCommodityType
